//
//  BKNoteListVC.m
//  lg_zhinengbeikao_english
//
//  Created by 盛超 on 2019/1/8.
//  Copyright © 2019年 zhukai. All rights reserved.
//

#import "BKNoteListVC.h"
#import "BKNoteListCell.h"
#import "BKSearchView.h"
#import "MBProgressHUD.h"
#import "BKNoteEidtVC.h"

@interface BKNoteListVC ()<UITableViewDelegate,UITableViewDataSource,UIScrollViewDelegate>

@property (nonatomic,strong) UITableView *table;
@property (nonatomic,strong) NSMutableArray *indexPaths;
@property (nonatomic,strong) NSMutableArray *dataArray;

@end

@implementation BKNoteListVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = kGrayColor;
    self.title = @"我的笔记(10份)";
    _dataArray = [NSMutableArray arrayWithArray:@[@1,@2,@3,@4,@5,@6,@7,@8,@9,@10]];
    self.indexPaths = [NSMutableArray array];
    
    [self setupUI];
    [self addLeftBackBarButtonItem];
}

- (UITableView *)table {
    if (!_table) {
        _table = [[UITableView alloc] init];
        _table.estimatedRowHeight = 200;
        _table.rowHeight = UITableViewAutomaticDimension;
        _table.backgroundColor = rgba(245, 245, 245, 1);
        _table.separatorStyle = UITableViewCellSeparatorStyleNone;
        _table.delegate = self;
        _table.dataSource = self;
        [_table registerClass:[BKNoteListCell class] forCellReuseIdentifier:@"BKNoteListCell"];
      
    }
    return _table;
}

- (void)setupUI {
    [self.view addSubview:self.table];
    [self.table mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
        if (@available(iOS 11.0, *)) {
            make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom);
            make.top.equalTo(self.view.mas_safeAreaLayoutGuideTop);
            make.left.equalTo(self.view.mas_safeAreaLayoutGuideLeft);
            make.right.equalTo(self.view.mas_safeAreaLayoutGuideRight);
        }
    }];
    self.table.mj_header = [KZRefreshHeader headerWithRefreshingBlock:^{
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self.table.mj_header endRefreshing];
        });
    }];
    self.table.mj_footer = [KZRefreshFooter footerWithRefreshingBlock:^{
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self.table.mj_footer endRefreshing];
        });
    }];
}

#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataArray.count;
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    BKNoteListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BKNoteListCell"];
    cell.indexPath = indexPath;
   
    cell.numtest = ((NSNumber*)_dataArray[indexPath.row]).integerValue;
    
    NSLog(@"cell.numtest = %ld", cell.numtest );
    
    WeakObj(self);
    cell.listBlock = ^(BOOL isTapDelete, BOOL isLeftSwipe, NSIndexPath * _Nonnull cellIndexPath) {
        
        //是否左滑
        if (isLeftSwipe) {//左滑
            
            if (![selfWeak.indexPaths containsObject:cellIndexPath]) {
                
                [selfWeak reumseCell];
                
                [selfWeak.indexPaths addObject:cellIndexPath];
            }
            
        } else {//右滑
            
            if ([selfWeak.indexPaths containsObject:cellIndexPath]) {
                [selfWeak.indexPaths removeObject:cellIndexPath];
            }
            
        }
        
        //是否删除
        if (isTapDelete) {
            [selfWeak.indexPaths removeObject:cellIndexPath];
            [selfWeak.dataArray removeObjectAtIndex:cellIndexPath.row];
            [selfWeak.table reloadData];
        }
        
    };
    
    [cell reumseCellAnimation:NO];
    
    return cell;
    
}


#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section==0) {
        return 47;
    } else {
        return 0;
    }
    
}

- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    //搜索框
    BKSearchView *searchView = [[BKSearchView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 47)];
    searchView.searchEndBlock = ^(NSString * _Nonnull searchText) {
        NSLog(@"%@",searchText);
    };
    return searchView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0;
}

- (UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [UIView new];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (self.indexPaths.count>0) {
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [self reumseCell];
            [self reumseCell];
            [self reumseCell];
        });
        
    } else {
        
//        MBProgressHUD *HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
//        HUD.detailsLabel.text = @"查看笔记";
//        [HUD hideAnimated:YES afterDelay:1];
        BKNoteEidtVC *vc = [[BKNoteEidtVC alloc] init];
        [self present:vc];
        
    }
    
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {

    [self reumseCell];
    
}

- (void)reumseCell {
    
    for (NSInteger i = 0; i<self.indexPaths.count; i++) {
        BKNoteListCell *cell = [self.table cellForRowAtIndexPath:self.indexPaths[i]];
        NSLog(@"%@",cell);
        [cell reumseCellAnimation:YES];
        [self.indexPaths removeObject:self.indexPaths[i]];
    }
    
}


@end
